package com.cjfather.redis.steam.annotation.consumers;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.stereotype.Component;

import com.cjfather.redis.steam.annotation.AnnoRedisStreamListener;
import com.cjfather.redis.steam.annotation.constants.RedisConstants;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author: todd
 * @date:   2023-08-08
 */

@Component
@Slf4j
@Scope("prototype")
@Data
public class TestMapStringStreamConsumer extends RedisTemplateResource implements AnnoMapStringConsumerI{
	/**
     * 消费组
     */
    private String group;
    /**
     * 消费组中的某个消费者
     */
    private String consumerName;
    
	private Gson gson = new GsonBuilder().create();


	@AnnoRedisStreamListener(streamKey = RedisConstants.STREAM_KEY, consumerGroup = RedisConstants.STREAM_GROUP, consumerName = RedisConstants.CONSUMER_NAME,consumerSize = 2)
	@Override
	public void onMessage(MapRecord<String, String, String> message) {
		log.info("{}",gson.toJson(message));
		String key = message.getStream();
		RecordId messageId = message.getId();
		Map<String, String> value = message.getValue();
		String vv = value.get("data").replaceAll("\\\\", "");
//		Map<String, Object> msgMain = gson.fromJson(vv, Map.class);
		redisStreamCommonService.delRecord(key,messageId);;
		log.info("key:{},组:{},消费者{} + stream message。messageId={},  body={}",key, group,consumerName,messageId, vv);
	}

}
